package com.ysm.smfu.activity;

import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;

import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.ThreadUtils;
import com.ysm.smfu.view.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/14.
 * 下拉刷新，上拉加载的基类
 */
public abstract class BaseRefreshActivity<T> extends BaseActivity {

    private long currentTimeMillis;
    private long lastRefreshTime;
    private RefreshLayout refreshLayout;
    private boolean isRefresh;
    private long currLoadTime;
    private double lastLoadTime;
    private boolean isLoad;
    protected int page = 1;
    //    刷新的数据源
    List<T> datas = new ArrayList<>();
    private List<T> preDatas;
    private int preSize;

    public void initRefresh(RefreshLayout refreshLayout, boolean isNeedLoad) {
        this.refreshLayout = refreshLayout;
        this.refreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        this.refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentTimeMillis = System.currentTimeMillis();
//                两次刷新时间需要大于1分钟
                if (currentTimeMillis - lastRefreshTime < SpKey.TIME_SPACE) {
//                    伪刷新
                    sleepRefresh();
                } else {
                    isRefresh = true;
                    datas.removeAll(preDatas);
                    load(false);
                    lastRefreshTime = currentTimeMillis;
                }
            }
        });
        if (isNeedLoad)
            this.refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
                @Override
                public void onLoad() {
                    currLoadTime = System.currentTimeMillis();
                    if (preSize == 0) {
//                两次刷新时间需要大于1分钟
                        if (currLoadTime - lastLoadTime < SpKey.TIME_SPACE) {
//                    伪刷新
                            sleepLoad();
                        } else {
                            isLoad = true;
                            load(true);
                            lastLoadTime = currLoadTime;

                        }
                    } else {
                        isLoad = true;
                        load(true);
                        lastLoadTime = currLoadTime;
                    }
                }
            });
    }

    /**
     * 伪刷新
     */
    private void sleepRefresh() {
        ThreadUtils.runOnBackThread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(SpKey.FAKE_TIME);
                ThreadUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.setRefreshing(false);
                    }
                });
            }
        });
    }

    /**
     * 伪加载
     */
    private void sleepLoad() {
        ThreadUtils.runOnBackThread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(SpKey.FAKE_TIME);
                ThreadUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.setLoading(false);
                    }
                });
            }
        });
    }

    /**
     * 设置加载到的数据
     */
    protected void setLoadData(List<T> preDatas) {
        if (preDatas == null) {//如果加载数据失败则返回
            preSize = 0;
            return;
        }
        if (preDatas.size() > 0) //如果加载到新数据，则记录下来
            this.preDatas = preDatas;
        preSize = preDatas.size(); // 记录下加载到的数据
        dealData();
    }

    /**
     * 加载完成时，处理数据
     */
    public void dealData() {
        if (page == 1) { //如果当前为第一页，则清空数据
            datas.clear();
        }
        setRefreshDone(); //设置刷新完成
        dealLoad();  // 处理加载结果
        if (preSize > 0) //如果加载到数据，则添加到数据集
            datas.addAll(preDatas);
    }

    /**
     * 设置刷新完成
     */
    protected void setRefreshDone() {
        if (isRefresh)
            isRefresh = false;
        refreshLayout.setRefreshing(false);
    }

    /**
     * 处理上拉加载失败情况
     */
    protected void dealLoadError() {
        if (isLoad) {
            isLoad = false;
            if (page > 1)
                page--;
            refreshLayout.setLoading(false);
        }
    }

    /**
     * 刷新失败
     */
    public void onRefreshError() {
        setRefreshDone();
        dealLoadError();
    }

    /**
     * 处理加载结果
     */
    private void dealLoad() {
        if (isLoad) {
            if (preSize == 0) {
                refreshLayout.setLoading(false);
                if (page > 1)
                    page--;
            } else {
                refreshLayout.setLoading(true);
            }
            isLoad = false;
        }
    }

    /**
     * 加载数据
     *
     * @param isLoad 是否是加载
     */
    public abstract void load(boolean isLoad);
}
